import request from '@/utils/request'
import {ApiResponse,PageData,PageQuery,BaseDetail} from "@/ts/CommonTypes"

import { downloadExcel } from '@/api/ApiUtils'

const urlPrefix = '/api/authUser/'


// ============ 接口定义 ============
export class AuthUser extends BaseDetail{
    accountNo: string = null;
    userName: string = null;
    nickName: string = null;
    roleId: number | null = null;
    organizationId: number | null = null;
    email: string = null;
    phone: string = null;
    roleName: string = null;
    organizationName: string = null;

    constructor(init?: Partial<AuthUser>) {
        super();
        if (init) {
            Object.assign(this, init);
        }
    }
}


export class QueryData extends PageQuery{
    organizationId?: number | null = null;
    roleId?: number | null = null;
    accountNo?: string | null = null;
    userName?: string | null = null;
    email?: string | null = null;
    phone?: string | null = null;
    constructor(init?: Partial<QueryData>) {
        super();
        if (init) {
            Object.assign(this, init);
        }
    }
}
// ------------------------------ API 方法 ------------------------------

/**
 * 添加用户
 */
export function add(data: AuthUser) {
    return request({
        url: urlPrefix + 'add',
        method: 'POST',
        data,
    })
}

/**
 * 登录
 */
export function login(data: { accountNo: string; password: string }) {
    return request({
        url: urlPrefix + 'login',
        method: 'POST',
        data,
    })
}

/**
 * 注册
 */
export function register(data: AuthUser) {
    return request({
        url: urlPrefix + 'register',
        method: 'POST',
        data,
    })
}

/**
 * 登出
 */
export function logout() {
    return request({
        url: urlPrefix + 'logout',
        method: 'POST',
    })
}

/**
 * 查询详情
 */
export function getById(id: number) {
    return request<ApiResponse<AuthUser>>({
        url: urlPrefix + 'getById',
        method: 'GET',
        params: { id },
    })
}

/**
 * 查询列表（分页）
 */
export function queryList(query: QueryData) {
    return request<ApiResponse<PageData<AuthUser>>>({
        url: urlPrefix + 'page',
        method: 'POST',
        data: query,
    })
}

/**
 * 导出 Excel
 */
export function exportList(query: QueryData) {
    return request
        .post(urlPrefix + 'export', query, {
            responseType: 'blob',
        })
        .then((res) => {
            downloadExcel(res, '用户列表.xlsx')
        })
}

/**
 * 保存用户（新增或编辑）
 */
export function save(data: AuthUser) {
    return request({
        url: urlPrefix + 'edit',
        method: 'POST',
        data,
    })
}

/**
 * 删除用户
 */
export function deleteById(id: number) {
    if (!id || id <= 0) {
        return null
    }
    return request({
        url: urlPrefix + 'deleteById',
        method: 'POST',
        params: { id },
    })
}

/**
 * 重置密码
 */
export function resetPasswordById(id: number) {
    if (!id || id <= 0) {
        return null
    }
    return request({
        url: urlPrefix + 'resetPasswordById',
        method: 'POST',
        params: { id },
    })
}
